//
//  BLDouYuLiveListViewController.swift
//  BLLiveTelecast
//
//  Created by BigL055 on 16/6/11.
//  Copyright © 2016年 bigL.liveTelecast.com. All rights reserved.
//

import UIKit
import MJRefresh

class BLDouYuLiveListViewController: UIViewController {
    
    //直播子频道 分类
    var subListCollectionView : UICollectionView?
    
    var subListArray = [BLDouyuRoomDetalModal]()
    
    let douyuSubListCellID = "BLDouyuSubListCellID"
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.grayColor()
        setupUI()
        
    }
    
    
}
// MARK: - 设置UI界面
extension BLDouYuLiveListViewController{
    
    func setupUI() {
        setupSubListView()
        getNetWorkData()
    }
    
    
    func setupSubListView() {
        subListCollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: BLCollectionIconFlowLayout())
        subListCollectionView?.backgroundColor = UIColor.grayColor()
        subListCollectionView?.delegate = self
        subListCollectionView?.dataSource = self
        subListCollectionView?.registerNib(UINib.init(nibName: String(BLDouyuSubListCell), bundle: nil), forCellWithReuseIdentifier: douyuSubListCellID)
        view.addSubview(subListCollectionView!)
        subListCollectionView?.snp_makeConstraints(closure: { (make) in
            make.top.right.left.bottom.equalToSuperview()
        })
        //下拉刷新
        subListCollectionView?.mj_header = MJRefreshNormalHeader(refreshingBlock: {
            self.getNetWorkData()
        })
        
    }
}

// MARK: - 网络请求
extension BLDouYuLiveListViewController{
    
    func getNetWorkData() {
        
        BLGetDouyuData.shareInstance.getSubDetailData("game") { (dataArray) in
            self.subListArray = dataArray!
            //结束下拉刷新
            self.subListCollectionView?.mj_header.endRefreshing()
            self.subListCollectionView?.reloadData()
        }
        
    }
    
}

// MARK: - UICollectionViewDelegate,UICollectionViewDataSource
extension BLDouYuLiveListViewController : UICollectionViewDelegate,UICollectionViewDataSource{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return subListArray.count
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(douyuSubListCellID, forIndexPath: indexPath) as! BLDouyuSubListCell
        cell.subListItem = subListArray[indexPath.item]
        return cell
        
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        let item = subListArray[indexPath.item]
        
        let tagID = item.cate_id
        let tagName = item.game_name
        
        let subListVC = BLSubListViewController.subListVC()
        subListVC.tagID = tagID
        subListVC.navTitleText = tagName!
        self.navigationController?.pushViewController(subListVC, animated: true)
        
        
    }
}

